/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyh.actionitembadge_ohos.other;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.text.Font;
import ohos.agp.text.RichText;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.text.TextForm;
import ohos.agp.utils.Color;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import com.zyh.actionitembadge_ohos.ResourceTable;

import java.util.List;

/**
 * @since 2021-06-11
 */
public class ListLeftAdapter extends BaseItemProvider {
    private List<LeftBean> listLeft;
    private Context mContext;
    private int mSelectedPos = -1;//实现单选  方法二，变量保存当前选中的position

    /**
     * 左侧的adapter构造器
     * @param mContext 上下文
     * @param listLeft  数据
     **/
    public ListLeftAdapter(Context mContext, List<LeftBean> listLeft) {
        this.listLeft = listLeft;
        this.mContext = mContext;
        richTextBuilder = new RichTextBuilder();
        for (int i = 0; i < listLeft.size(); i++) {
            if (listLeft.get(i).isCheck()) {
                mSelectedPos = i;
            }
        }
    }

    private RichTextBuilder richTextBuilder;

    @Override
    public int getCount() {
        return listLeft.size();
    }

    @Override
    public Object getItem(int position) {
        return listLeft.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final ViewHolder viewHolder;
        Component component1 = component;
        if (component1 == null) {
            component1 = LayoutScatter.getInstance(mContext)
                .parse(ResourceTable.Layout_left_content, null, false);
            viewHolder = new ViewHolder();
            viewHolder.text = (Text) component1.findComponentById(ResourceTable.Id_left_item_content);
            component1.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component1.getTag();
        }
        viewHolder.text.setText(listLeft.get(position).getFuncdescribe());
        TextForm textForm = new TextForm();

        if (listLeft.get(position).isCheck() == true) {
            viewHolder.text.setTextColor(new Color(Color.getIntColor("#329df4")));
            textForm.setTextColor(Color.getIntColor("#329df4"));
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#e4f2fe")));
            viewHolder.text.setBackground(shapeElement);
        } else {
            viewHolder.text.setTextColor(new Color(Color.getIntColor("#000000")));
            textForm.setTextColor(Color.getIntColor("#000000"));
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#fefefe")));
            viewHolder.text.setBackground(shapeElement);
        }

        textForm.setTextSize(AttrHelper.vp2px(15, mContext));
        richTextBuilder.mergeForm(textForm);
        richTextBuilder.addText(listLeft.get(position).getFuncdescribe());
        richTextBuilder.revertForm();
        RichText richText = richTextBuilder.build();
        viewHolder.text.setRichText(richText);

        viewHolder.text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                for (LeftBean data : listLeft) {
                    data.setCheck(false);
                }
                listLeft.get(position).setCheck(true);
                mItemOnClickListener.itemOnClickListener(position);
                notifyDataChanged();

            }
        });
        return component1;
    }

    private ItemOnClickListener mItemOnClickListener;

    /***
     * 设置Item监听器
     * @param listener item监听器
     *
     **/
    public void setmItemOnClickListener(ItemOnClickListener listener) {
        this.mItemOnClickListener = listener;
    }

    /**
     * Item监听器接口
     *
     **/
    public interface ItemOnClickListener {
        /**
         * 传递点击的view 的position
         *
         * @param position
         */
        public void itemOnClickListener(int position);
    }

    private class ViewHolder {
        Text text;
    }

}
